#include "ws2812.h"
#include <math.h>
#include <stdbool.h>
#include "ch32v00x_conf.h"
//TODO：停止码

uint16_t ws_send_buffer[WS_BUFFER_LEN] = {0};


void ws2812_turn_off_all(){
    uint16_t i = 0;
    for(i = 0; i < WS2812_NUM*24; i++){
        ws_send_buffer[i] = WS0;   //全部为逻辑0
    }
    for(i = WS2812_NUM * 24; i < WS_BUFFER_LEN; i++){
        ws_send_buffer[i] = 0;     //停止信号
    }
    ws2812_apply();
}

void ws2812_set_color_all(uint32_t color_rgb){
    uint8_t n, i = 0;
    
    for(n = 0; n < WS2812_NUM; n++){
        for(i = 0; i < 24; i++){
            ws_send_buffer[24 * n + i] = ( (color_rgb << i) & 0x800000 ) ? WS1 : WS0;
        }
    }
}

void ws2812_set_color_pixel(uint16_t n, uint32_t color_rgb){
    if(n > WS2812_NUM) return;

    //转换成编码数据
    uint8_t i = 0;
    for(i = 0; i < 24; i++){
        //一位位比较
        ws_send_buffer[24 * n + i] = ( (color_rgb << i) & 0x800000 ) ? WS1 : WS0;
    }
}

void ws2812_apply(){
    DMA_SetCurrDataCounter(DMA1_Channel5, WS_BUFFER_LEN);
    DMA_Cmd(DMA1_Channel5, ENABLE);
    TIM_Cmd(TIM1, ENABLE);

    while(DMA_GetFlagStatus(DMA1_FLAG_TC5) == RESET);

    TIM_Cmd(TIM1, DISABLE);
    DMA_Cmd(DMA1_Channel5, DISABLE);
    DMA_ClearFlag(DMA1_FLAG_TC5);
}

void ws2812_rainbow_wheel_mode() {

    static color_hsv_t color_hsv = {
            .h = 0,
            .s = 1.0f,
            .v = 1.0f
    };

    static color_rgb_t color_rgb = {0};

    static uint16_t h = 0;


    uint8_t i;
    for(i = 0; i < WS2812_NUM; i++){
        color_hsv.h = ((h + 5*i) % 255);
        hsv2rgb(&color_hsv, &color_rgb);
        ws2812_set_color_pixel(i, color_rgb.color);
    }

    ws2812_apply();

    h += 2;
    h = h % 255;
}

void ws2812_rainbow_breathe_mode(){
    static uint32_t i = 0;
    static uint8_t step = 0;
    static uint32_t color = 0xFF0000;
    
    switch (step){
    case 0:
        //FF0000到ffff00 红变黄

        color += 0x100;
        ws2812_set_color_all(color);
        ws2812_apply();

        i++;
        if(i == 0xFF){
            step++;
            i = 0;
        }
        break;

    case 1:
        //ffff00到00ff00 黄变绿

        color -= 0x10000;
        ws2812_set_color_all(color);
        ws2812_apply();

        i++;
        if(i == 0xFF){
            step++;
            i = 0;
        }
        break;
    

    case 2:
   	    //00ff00 到00ffff 绿变青
        color++;
        ws2812_set_color_all(color);
        ws2812_apply();

        i++;
        if(i == 0xFF){
            step++;
            i = 0;
        }

        break;

    case 3:
   	    //00ffff 到0000ff 青变蓝
        color -= 0x100;
        ws2812_set_color_all(color);
        ws2812_apply();

        i++;
        if(i == 0xFF){
            step++;
            i = 0;
        }
        break;
    
    case 4:
   	    //0000ff 到ff00ff 蓝变紫
        color += 0x10000;
        ws2812_set_color_all(color);
        ws2812_apply();

        i++;
        if(i == 0xFF){
            step++;
            i = 0;
        }
        break;
    
    case 5:
   	    //ff00ff 到0000ff 紫变红
        color --;
        ws2812_set_color_all(color);
        ws2812_apply();

        i++;
        if(i == 0xFF){
            step = 0;
            i = 0;
        }
        break;
    }

}

void ws2812_single_color_mode(){
//    static bool sent = false;
//    if(sent) return;

    ws2812_set_color_all(_rgb(0xF3,0xF9,0xFF));
    ws2812_apply();

    //sent = true;
}

void ws2812_single_color_breathe_mode(){
    static color_hsv_t color_hsv = {
        .h = 240,
        .s = 0.2,
        .v = 1
    };

    static color_rgb_t color_rgb = {0};
    static uint8_t mode = 0;    //饱和度由低变高，高变低

    switch (mode){
    case 0:
        color_hsv.s += 0.005;
        if(color_hsv.s >= 1){
            color_hsv.s = 1;
            mode = 1;
        }
        break;
    case 1:
        color_hsv.s -= 0.005;
        if(color_hsv.s <= 0.1){
            color_hsv.s = 0.1;
            mode = 0;
        }
        break;
    }

    hsv2rgb(&color_hsv, &color_rgb);

    ws2812_set_color_all(color_rgb.color);
    ws2812_apply();
}

/*********************************************************************************************
RGB转化到HSV的算法:
    max=max(R,G,B)；
    min=min(R,G,B)；
    V=max(R,G,B)；
    S=(max-min)/max；
    if (R = max) H =(G-B)/(max-min)* 60；
    if (G = max) H = 120+(B-R)/(max-min)* 60；
    if (B = max) H = 240 +(R-G)/(max-min)* 60；
    if (H < 0) H = H + 360；
***********************************************************************************************/


#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max3(a,b,c) (((a) > (b) ? (a) : (b)) > (c) ? ((a) > (b) ? (a) : (b)) : (c))
#define min3(a,b,c) (((a) < (b) ? (a) : (b)) < (c) ? ((a) < (b) ? (a) : (b)) : (c))

/**
 * @brief   RGB颜色空间 转  HSV颜色空间
 * @param   rgb:RGB颜色空间参数
 * @param   hsv:HSV颜色空间参数
 * @note    The R,G,B values are divided by 255 to change the range from 0..255 to 0..1:
 * @return  none
*/
void rgb2hsv(color_rgb_t *rgb, color_hsv_t *hsv)
{
    float max, min, delta=0;
    float r = (float)((float)((int)rgb->r)/255);
    float g = (float)((float)((int)rgb->g)/255);
    float b = (float)((float)((int)rgb->b)/255);

    max = max3(r, g, b);
    min = min3(r, g, b);
    delta = (max - min);

    //printf("r:%f, g:%f, b:%f\n", r, g, b);

    if (delta == 0) {
        hsv->h = 0;
    } else {
        if (r == max) {
            hsv->h = ((g-b)/delta)*60; 
        } else if (g == max) {
            hsv->h = 120+(((b-r)/delta)*60); 
        } else if (b == max) {
            hsv->h = 240 + (((r-g)/delta)*60);
        }

        if (hsv->h < 0) {
            hsv->h += 360;
        }
    }

    if (max == 0) {
        hsv->s = 0;
    } else {
        hsv->s = (float)(delta/max);
    }

    hsv->v = max;

    rgb->color = (unsigned int)(((rgb->r&0xff)<<16) | ((rgb->g&0xff)<<8) | (rgb->b&0xff));
}


/*************************************************************************
HSV转化到RGB的算法:
    if (s = 0)
    R=G=B=V;
    else
    H /= 60;
    i = INTEGER(H);
    f = H - i;
    a = V * ( 1 - s );
    b = V * ( 1 - s * f );
    c = V * ( 1 - s * (1 - f ) );
    switch(i)
    case 0: R = V; G = c; B = a;
    case 1: R = b; G = v; B = a;
    case 2: R = a; G = v; B = c;
    case 3: R = a; G = b; B = v;
    case 4: R = c; G = a; B = v;
    case 5: R = v; G = a; B = b;
*******************************************************************************/

/**
 * @brief   HSV颜色空间 转 RGB颜色空间
 * @param   hsv:HSV颜色空间参数
 * @param   rgb:RGB颜色空间参数
 * @note    When 0 ≤ H < 360, 0 ≤ S ≤ 1 and 0 ≤ V ≤ 1:
 * @return  none
*/
void hsv2rgb(color_hsv_t *hsv, color_rgb_t *rgb)
{
    int i;
    float f,a,b,c;

    float h = hsv->h;
    float s = hsv->s;
    float v = hsv->v;
	if (h >= 360) {
		h = 0;
	}
	
    if (s == 0) {
        rgb->r = (unsigned char)((int)(v*255));
        rgb->g = (unsigned char)((int)(v*255));
        rgb->b = (unsigned char)((int)(v*255));
    } else {
        h /= 60.0;  // sector 0 to 5, h_max=360 360/60=6[0,1,2,3,4,5]
        i = (int)h; // floor(h)
        f = h-i; // factorial path of h
        a = v * (1-s);
        b = v * (1-s*f);
        c = v * (1-s*(1-f));
        switch(i) {
            case 0:
                rgb->r = (unsigned char)((int)(v*255)); //v*255
                rgb->g = (unsigned char)((int)(c*255)); //c*255;
                rgb->b = (unsigned char)((int)(a*255)); //a*255;
                break;
            case 1:
                rgb->r = (unsigned char)((int)(b*255)); //b*255;
                rgb->g = (unsigned char)((int)(v*255)); //v*255;
                rgb->b = (unsigned char)((int)(a*255)); //a*255;
                break;
            case 2:
                rgb->r = (unsigned char)((int)(a*255)); //a*255;
                rgb->g = (unsigned char)((int)(v*255)); //v*255;
                rgb->b = (unsigned char)((int)(c*255)); //c*255;
                break;
            case 3:
                rgb->r = (unsigned char)((int)(a*255));//a*255;
                rgb->g = (unsigned char)((int)(b*255));//b*255;
                rgb->b = (unsigned char)((int)(v*255));//v*255;
                break;
            case 4:
                rgb->r = (unsigned char)((int)(c*255)); //c*255;
                rgb->g = (unsigned char)((int)(a*255)); //a*255;
                rgb->b = (unsigned char)((int)(v*255)); //v*255;
                break;
            default:
                rgb->r = (unsigned char)((int)(v*255)); //v*255;
                rgb->g = (unsigned char)((int)(a*255)); //a*255;
                rgb->b = (unsigned char)((int)(b*255)); //b*255;
                break;
        }

    }

    rgb->color = (unsigned int)(((rgb->r&0xff)<<16) | ((rgb->g&0xff)<<8) | (rgb->b&0xff));
}

/**
 * @brief   调节HSV颜色空间 H值(0.0~360.0)
 * @param   hsv:HSV颜色空间参数
 * @param   h_offset:调节参数 增加>0.0，减小<0.0
 * @return  none
*/
void hsv_adjust_h(color_hsv_t *hsv, float h_offset)
{
    hsv->h += h_offset;
    if (hsv->h >= 360) {
        hsv->h = 360;
    } else if (hsv->h<=0) {
        hsv->h = 0;
    }
}


/**
 * @brief   调节HSV颜色空间 S值(0.0~1.0)
 * @param   hsv:HSV颜色空间参数
 * @param   s_offset:调节参数 增加>0.0，减小<0.0
 * @return  none
*/
void hsv_adjust_s(color_hsv_t *hsv, float s_offset)
{
    hsv->s += s_offset;
    if (hsv->s >= 1.0) {
        hsv->s = 1.0;
    } else if (hsv->s <= 0) {
        hsv->s = 0;
    }
}


/**
 * @brief   调节HSV颜色空间 V值(0.0~1.0)
 * @param   hsv:HSV颜色空间参数
 * @param   v_offset:调节参数 增加>0.0，减小<0.0
 * @return  none
*/
void hsv_adjust_v(color_hsv_t *hsv, float v_offset)
{
    hsv->v += v_offset;
    if (hsv->v >= 1.0) {
        hsv->v = 1.0;
    } else if (hsv->v<=0) {
        hsv->v = 0;
    }
}
